package com.leadbank.bigdata.mapreduce;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * Created by hp on 2018/4/27.
 */
public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable>{

    //生命周期：框架每传递进来一个kv 组，reduce方法被调用一次
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {

        // 显示次数表示redcue函数被调用了多少次，表示k2有多少个分组
        System.out.println("Reducer输入分组<" + key.toString() + ",N(N>=1)>");
        //定义一个计数器
        int count = 0;
        //遍历这一组kv的所有v，累加到count中
        for(IntWritable value:values){
            count += value.get();
            // 显示次数表示输入的k2,v2的键值对数量
            System.out.println("Reducer输入键值对<" + key.toString() + ","
                    + value.get() + ">");
        }
        context.write(key, new IntWritable(count));
    }

}
